<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * viewevent.php
 * Began: Fri December 20 2002
 * 
 * $Id: viewevent.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/

define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

// Start session management
$user->start();
$user->setup();
$user->check_auth('u_event_view');

if ( (isset($_GET[URI_EVENT])) && (intval($_GET[URI_EVENT] > 0)) )
{
    $sort_order = array(
        0 => array('raid_date desc', 'raid_date'),
        1 => array('raid_note', 'raid_note desc'),
        2 => array('raid_value desc', 'raid_value')
    );
     
    $current_order = switch_order($sort_order);

    $sql = 'SELECT event_id, event_name, event_value, event_added_by, event_updated_by
            FROM ' . EVENTS_TABLE . "
            WHERE event_id='".$_GET[URI_EVENT]."'";
            
    if ( !($event_result = $db->query($sql)) )
    {
        message_die('Could not obtain event information', '', __FILE__, __LINE__, $sql);
    }
    
    // Check for a valid event
    if ( !$event = $db->fetch_record($event_result) )
    {
        message_die($user->lang['error_invalid_event_provided']);
    }
    
    // Init vars used to get averages and totals
    $total_drop_count = 0;
    $total_attendees_count = 0;
    $total_earned = 0;
    
    $sql = 'SELECT raid_id, raid_date, raid_note, raid_value 
            FROM ' . RAIDS_TABLE . "
            WHERE raid_name='".$event['event_name']."' ORDER BY ".$current_order['sql'];
    
    if ( !$raids_result = $db->query($sql) )
    {
        message_die('Could not obtain raid information', '', __FILE__, __LINE__, $sql);
    }
    while ( $raid = $db->fetch_record($raids_result) )
    {
        $drop_count = $db->query_first('SELECT count(*) FROM ' . ITEMS_TABLE . " WHERE raid_id='".$raid['raid_id']."'");
        
        $sql = 'SELECT count(*)
                FROM ' . RAID_ATTENDEES_TABLE . "
                WHERE raid_id='".$raid['raid_id']."'";
        $attendees_count = $db->query_first($sql);

        $tpl->assign_block_vars('raids_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'U_VIEW_RAID' => 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$raid['raid_id'],
            'DATE' => ( !empty($raid['raid_id']) ) ? date($user->style['date_notime_short'], $raid['raid_date']) : '&nbsp;',
            'ATTENDEES' => $attendees_count,
            'DROPS' => $drop_count,
            'NOTE' => ( !empty($raid['raid_note']) ) ? stripslashes($raid['raid_note']) : '&nbsp;',
            'VALUE' => $raid['raid_value'])
        );
        
        // Add the values of this row to our totals
        $total_drop_count += $drop_count;
        $total_attendees_count += $attendees_count;
        $total_earned += $raid['raid_value'];       
    }
    
    $total_raid_count = $db->num_rows($raids_result);
    
    // Prevent div by 0
    $average_attendees = ( $total_raid_count > 0 ) ? floor($total_attendees_count / $total_raid_count) : '0';
    $average_drops = ( $total_drop_count > 0 ) ? floor($total_drop_count / $total_raid_count) : '0';
    
    $db->free_result($raids_result);
    
    $tpl->assign_vars(array(
        'L_RECORDED_RAID_HISTORY' => sprintf($user->lang['recorded_raid_history'], stripslashes($event['event_name'])),
        'L_ADDED_BY' => $user->lang['added_by'],
        'L_UPDATED_BY' => $user->lang['updated_by'],
        'L_DATE' => $user->lang['date'],
        'L_ATTENDEES' => $user->lang['attendees'],
        'L_DROPS' => $user->lang['drops'],
        'L_NOTE' => $user->lang['note'],
        'L_VALUE' => $user->lang['value'],
        'L_AVERAGE' => $user->lang['average'],
        'L_TOTAL_EARNED' => $user->lang['total_earned'],
        
        'O_DATE' => $current_order['uri'][0],
        'O_NOTE' => $current_order['uri'][1],
        'O_VALUE' => $current_order['uri'][2],
        
        'U_VIEW_EVENT' => 'viewevent.php'.$SID.'&amp;' . URI_EVENT . '='.$_GET[URI_EVENT].'&amp;',
        
        'EVENT_ADDED_BY' => ( !empty($event['event_added_by']) ) ? $event['event_added_by'] : 'N/A',
        'EVENT_UPDATED_BY' => ( !empty($event['event_updated_by']) ) ? $event['event_updated_by'] : 'N/A',
        'ROW_CLASS' => $eqdkp->switch_row_class(),
        'AVERAGE_ATTENDEES' => $average_attendees,
        'AVERAGE_DROPS' => $average_drops,
        'TOTAL_EARNED' => sprintf("%.2f", $total_earned),
        'VIEWEVENT_FOOTCOUNT' => sprintf($user->lang['viewevent_footcount'], $total_raid_count))
    );
    
    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.sprintf($user->lang['viewevent_title'], stripslashes($event['event_name']));
    include_once($eqdkp_root_path . 'includes/page_header.php');
    
    $tpl->set_filenames(array(
        'body' => 'viewevent.html')
    );
    
    include_once($eqdkp_root_path . 'includes/page_tail.php');
}
else
{
    message_die($user->lang['error_invalid_event_provided']);
}
?>